package com.array;


import java.util.Stack;

public class Array<E> {
    private E data[];
    private int size;

    /**
     * @param capacity 数组容量
     */
    public Array(int capacity){
        this.data = (E[]) new Object[capacity];
    }

    /**
     * 无参构造函数，容量默认10
     */
    public Array(){
        this(10);
    }

    public int getSize() {
        return size;
    }

    /**
     * 获取数组容量
     * @return
     */
    public int getCapacity(){
        return data.length;
    }

    /**
     * 数组是否为空
     * @return
     */
    public boolean isEmpty(){
        return size == 0;
    }

    /**
     * 向数组中插入一个元素
     * @param index
     * @param e
     */
    public void add(int index, E e){

        if(index < 0 || index > size)
            throw new IllegalArgumentException("Add Faild. Require index > 0 and index <= size");

        if(size == getCapacity())
            reSize(2 * getCapacity());

        for(int i = size - 1; i >= index; i--){
            data[i+1] = data[i];
        }
        data[index] = e;
        size++;
    }

    /**
     * 向数组尾添加元素
     * @param e
     */
    public void addLast(E e){
        add(size, e);
    }

    /**
     * 向数组头插入元素
     * @param e
     */
    public void addFirst(E e){
        add(0, e);
    }

    /**
     * 查找元素所在位置
     * @param e
     * @return
     */
    public int find(E e){
        for(int i = 0; i < size; i++){
            if(data[i] == e)
                return i;
        }
        return -1;
    }

    /**
     * 获取元素
     * @param index
     * @return
     */
    public E get(int index) {
        if (index < 0 || index >= size) {
            throw new IllegalArgumentException("Get Faild:Index is illegal");
        }
        return data[index];
    }
    public E getFirst(){
        return get(0);
    }
    public E getLast(){
        return get(size - 1);
    }

    /**
     * 删除
     * @param index
     * @return
     */
    public E remove(int index){
        if(index < 0 || index >= size)
         throw new IllegalArgumentException("Remove faild: must index > 0 and index < size");
        E res = data[index];
        for(int i = index; i < size -1; i++){
            data[i] = data[i+1];
        }
        size--;
        data[size] = null;//让垃圾回收器把对象回收了
        //动态缩减容量 当实际使用量是容量的四分之一时缩小容量，缩小二分之一，预留出四分之一的空间，防止复杂度震荡
        if(size == getCapacity()/4)
            reSize(getCapacity()/2);

        Stack stack = new Stack();
        return res;
    }

    /**
     * 删除元素
     * @param e
     * @return
     */
    public int removeElement(E e){
        int index = find(e);
        if(index != -1){
            remove(index);
        }
        return index;
    }

    /**
     * @return
     */
    public E removeLast(){
        return remove(size -1);
    }

    public E removeFirst(){
        return remove(0);
    }


    private void reSize(int newCapacity){
            E [] newData = (E[]) new Object[newCapacity];
            for (int i = 0; i < size; i++)
                newData[i] = data[i];
            data = newData;
    }

    @Override
    public String toString() {
        StringBuffer arr = new StringBuffer();
        arr.append(String.format("Array: size = %d , capacity = %d \n", size, getCapacity()));
        arr.append("[");

        for(int i = 0; i < size; i++){
            arr.append(data[i]);
            if(i != size -1)
                arr.append(",");
        }
        arr.append("]");
        return arr.toString();
    }
}
